package tcl.hujb.testitem;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemView extends FrameLayout {

	private String TAG = "ItemView";
	View mRootView;
	ImageView mImageView;
	TextView mTextView;
	ScaleAnimation mScaleAnimation;
	AlphaAnimation mAlphaAnimation;
	AnimationSet mAnimationSet;

	public void init(Context context) {
		mRootView = inflate(context, R.layout.itemview, null);
		mImageView = (ImageView) mRootView.findViewById(R.id.imageview);
		mTextView = (TextView) mRootView.findViewById(R.id.textview);
		mScaleAnimation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f);
		mScaleAnimation.setDuration(200);
		mScaleAnimation.setFillAfter(true);
		
		mAlphaAnimation = new AlphaAnimation(0.0f, 1.0f);
		mAlphaAnimation.setDuration(200);
		mAlphaAnimation.setFillAfter(true);
//		mAnimationSet = new AnimationSet(true);
//		mAnimationSet.addAnimation(mScaleAnimation);
//		mAnimationSet.addAnimation(mAlphaAnimation);
//		mAnimationSet.setFillAfter(true);
		
		addView(mRootView);
		setBackgroundColor(Color.TRANSPARENT);
		setFocusable(true);
	}
	
	public ItemView(Context ctx, AttributeSet attr){
		super(ctx, attr);
		init(ctx);
	}

	public void setImage(int res) {
		mImageView.setImageResource(res);
	}
	
	public void setText(String text) {
		mTextView.setText(text);
	}
	
	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
		Log.d(TAG, "-----------onFocusChanged-------------");
		if (gainFocus) {
			setBackgroundColor(Color.YELLOW);
			mTextView.setVisibility(View.VISIBLE);
			startAnimation(mScaleAnimation);
		}
		else {
			setBackgroundColor(Color.TRANSPARENT);
			mTextView.setVisibility(View.INVISIBLE);
			mScaleAnimation.cancel();
//			List<Animation> list = mAnimationSet.getAnimations();
//			for (Animation anim: list)
//				anim.reset();
		}
	}
}
